<template>
  <div>
    <div class="top-panel__box">
      <el-form :model="dataForm" ref="dataForm" inline size="small" onsubmit="return false">
          <el-row>
              <el-col :span="5">
                  <el-form-item class="business-name">
                    <el-input placeholder="输入受理机构名称/编号/负责人" v-model="dataForm.orgInfo" maxlength="30" clearable />
                  </el-form-item>
              </el-col>
              <el-col :span="3">
                  <el-form-item>
                    <el-input placeholder="输入归属机构编号" v-model="dataForm.organizationCode" maxlength="30" clearable />
                </el-form-item>
              </el-col>
              <el-col :span="5">
                  <el-form-item>
                    <el-button type="primary" @click="getList(1)">
                        <fa-icon class="fa-search" />查询</el-button>
                    <el-button plain type="" @click="onReset">
                      <fa-icon class="fa-refresh" />重置
                    </el-button>
                  </el-form-item>
              </el-col>
          </el-row>
      </el-form>
    </div>
    <el-row class="button-box">
      <el-col>
        <el-button type="primary" size="small" @click="addPerEvent" v-permission="'DSYJ_ADD'">
          <fa-icon class="fa-plus" />新增归属配置</el-button>
        <tooltip :tooltipOption="tooltipOption" />
      </el-col>
    </el-row>
    <el-row class="table-top">
      <el-table :data="dataList" v-loading="tableLoading" tooltip-effect="light" class="tc-table" size="small">
        <el-table-column type="index" width="55" fixed="left" label="序号"></el-table-column>
        <template v-for="(column, index) in DS_HEAD_LIST">
          <el-table-column :prop="column.prop" :label="column.label" :key="index" :width="column.width" :show-overflow-tooltip="true" />
        </template>
        <el-table-column prop="operation" class-name="oper-col" label="操作" :width="230">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" plain @click="editRow(scope.row, scope.$index)" v-permission="'DSYJ_MODIFY'">修改挂靠机构</el-button>
            <el-button size="mini" type="danger" plain @click="delPreRelateEvent(scope.row, scope.$index)" v-permission="'DSYJ_DELETE'">移除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-row class="pagination-top">
        <pagination :pageNum.sync="dataForm.pageNum" :curSizes.sync="dataForm.pageSize" :total="total" @getList="getList" />
      </el-row>
    </el-row>
    <el-dialog :title="editData.organizationId ? '修改挂靠机构' :'新增归属配置'" :visible.sync="dialog.addBranchDialog" width="40%" :close-on-click-modal="false">
      <add-performance
      v-if="dialog.addBranchDialog"
      :editData="editData"
      :isEdit="isEdit"
      @showAddUser="showBranchList"
      :listValue="perlist"
      @updataSuccess="getBranchData"
      @close="dialog.addBranchDialog = false"/>
    </el-dialog>
    <el-dialog title="业绩归属机构" :visible.sync="dialog.branchDialog" width="30%" :close-on-click-modal="false">
      <add-branch-list
      v-if="dialog.branchDialog"
      :valueList="perlist"
      @updataSuccess="updateInfo"
      @close="dialog.branchDialog = false"
      />
    </el-dialog>
  </div>
</template>

<script>
import api from '../api'
import { CONSTANT_OBJ } from '@/utils/constant'
import { DS_HEAD_LIST } from '../js/contant.js'
import AddPerformance from './AddPerformance'
import AddBranchList from './AddBranchList'
import { common } from '@/utils/common'
export default {
  components: { AddPerformance, AddBranchList },
  data () {
    return {
      DS_HEAD_LIST,
      tableLoading: false,
      dataForm: {
        orgInfo: '',
        organizationCode: '',
        pageNum: 1,
        pageSize: 10
      },
      perlist: [],
      isEdit: false,
      dialog: {
        addBranchDialog: false,
        branchDialog: false
      },
      editData: {},
      dataList: [],
      total: 0,
      tooltipOption: {
        title: '帮助提示',
        options: [
          '1、DS团队进件，需将业绩挂靠至分行及分行客户经理名下。本功能实现业绩归属关系配置。',
          '2、配置业绩归属关系，需（1）配置分行及分行客户经理名单（2）配置DS团队业绩挂靠的分行。',
          '3、若一个DS团队挂靠多个分行，上送业绩时随机上送至其中一个分行处。',
          '4、若一个分行配置多个客户经理，上送时业绩随机上送至其中一位客户经理处。',
          '5、当前有DS团队挂靠的分行无法被删除，需删除所有挂靠关系后才可删除分行。'
        ]
      }
    }
  },
  mounted () {
    this.getList()
  },
  methods: {
    // 查询列表
    getList (val) {
      this.dataForm.pageNum = val || this.dataForm.pageNum
      this.tableLoading = true
      api.queryPreformanceRelateList(this.dataForm).then(res => {
        const { responseCode, data } = res
        if (responseCode === CONSTANT_OBJ.RES_SUCCESS) {
          data.list.forEach(ele => {
            let result = []
            ele.performanceOrgList.forEach(v => {
              result.push(`${v.organizationName}(${v.organizationCode})`)
              ele.newPreforName = result.join(',')
            })
          })
          console.log(data.list, '机构名称')
          this.dataList = data.list || []
          this.total = parseInt(data.total) || 0
        }
      }).finally(() => {
        this.tableLoading = false
      })
    },
    editRow (row) {
      this.isEdit = true
      this.dialog.addBranchDialog = true
      this.editData = {...row}
      this.perlist = [...row.performanceOrgList]
    },
    addPerEvent () {
      this.editData = {}
      this.perlist = []
      this.dialog.addBranchDialog = true
      this.isEdit = false
    },
    showBranchList () {
      this.dialog.branchDialog = true
    },
    updateInfo (data) {
      this.perlist = [...data]
      this.dialog.branchDialog = false
    },
    getBranchData (data) {
      console.log(data, '提交参数')
      if (!this.isEdit) {
        this.addPreformanceRelateEvent(data)
      } else {
        this.editPreRelateEvent(data)
      }
    },
    addPreformanceRelateEvent (data) {
      api.addPreformanceRelate(data).then(res => {
        this.successFn(res, '')
        this.dialog.addBranchDialog = false
      })
    },
    editPreRelateEvent (data) {
      api.editPreformanceRelate(data).then(res => {
        this.successFn(res, '')
        this.dialog.addBranchDialog = false
      })
    },
    delPreRelateEvent (row) {
      common.confirmFn(`请确认是否删除 ${row.organizationName}(${row.organizationCode}) 机构的业绩归属配置？`, '删除归属配置').then(res => {
        if (res) {
          api.deletePreformanceRelate(row.organizationId).then(res => {
            this.successFn(res)
          })
        }
      })
    },
    onReset () {
      this.dataForm = {
        orgInfo: '',
        organizationCode: '',
        pageNum: 1,
        pageSize: 10
      }
    },
    successFn (res, dialog) {
      const { responseCode, responseMsg: message } = res
      if (responseCode === CONSTANT_OBJ.RES_SUCCESS) {
        this.$message({
          message,
          type: 'success'
        })
        if (dialog) {
          this[dialog] = false
        }
        this.getList(1)
      }
    }
  }
}
</script>
<style>
.business-name {
    width: 100%;
    margin-right: 20px;
}
.business-name .el-form-item__content{
    width: 95%;
}
.button-box{
  margin-bottom: 30px;
}
</style>
